package qx.hyj.demos.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qx.hyj.demos.entity.Book;
import qx.hyj.demos.entity.BookJl;
import qx.hyj.demos.entity.Ubjl;
import qx.hyj.demos.entity.User;
import qx.hyj.demos.enums.ErrorCode;
import qx.hyj.demos.excep.LibraryException;
import qx.hyj.demos.mapper.BookMapper;
import qx.hyj.demos.mapper.Usermapper;
import qx.hyj.demos.service.BookService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private Usermapper usermapper;
    @Autowired
    private BookMapper bookmapper;

    @Override
    public void borrowBook(String userName, String bookName) throws LibraryException {
        User user = usermapper.getIdByName(userName);
        Book book = bookmapper.getIdByName(bookName);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = sdf.format(date);
        if (book == null) {
            throw new LibraryException(ErrorCode.BOOK_NOT_FOUND);
        } else if (user == null) {
            throw new LibraryException(ErrorCode.USER_NOT_FOUND);
        } else if (bookmapper.getUsjl(user.getUserId(), book.getId()) != null) {
            throw new LibraryException(ErrorCode.BOOK_HAVE);
        } else if (book.getStatus().equals(ErrorCode.BORROWED)) {
            throw new LibraryException(ErrorCode.BORROWED);
        } else if ((user.getType() == 1 && user.getBookSize() >= 3) || (user.getType() == 2 && user.getBookSize() >= 10)) {
            throw new LibraryException(ErrorCode.BOOK_CAOCHUAN);
        } else if (user.getType() == 1 && user.getBookSize() < 3) {
            bookmapper.borrowBook(user.getUserId(), book.getId(), borrowTime);
            usermapper.updateBookSize(user.getUserId(), user.getBookSize() + 1);
            bookmapper.updateBookStock(book.getId(), book.getStock() - 1);
            if (book.getStock() == 1) {
                bookmapper.updateBookStatus(book.getId(), ErrorCode.BORROWED);
            }

        } else if (user.getType() == 2 && user.getBookSize() < 10) {
            bookmapper.borrowBook(user.getUserId(), book.getId(), borrowTime);
            usermapper.updateBookSize(user.getUserId(), user.getBookSize() + 1);
            bookmapper.updateBookStock(book.getId(), book.getStock() - 1);
            if (book.getStock() == 1) {
                bookmapper.updateBookStatus(book.getId(), ErrorCode.BORROWED);
            }
        }
    }

    @Override
    public int returnBook(String userName, String bookName) throws LibraryException {
        User user = usermapper.getIdByName(userName);
        Book book = bookmapper.getIdByName(bookName);
        int money;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String returnTime = sdf.format(date);
        String borrowTime = bookmapper.getUsjl(user.getUserId(), book.getId()).getBorrowTime();
        Integer JL = bookmapper.returnBook(user.getUserId(), book.getId());
        if (JL == 0) {
            throw new LibraryException(ErrorCode.NOT_FOUND_RECODE);
        } else {
            usermapper.updateBookSize(user.getUserId(), user.getBookSize() - 1);
            bookmapper.updateBookStock(book.getId(), book.getStock() + 1);
            if (book.getStatus().equals(ErrorCode.BORROWED)) {
                bookmapper.updateBookStatus(book.getId(), ErrorCode.AVAILABLE);
            }
            String[] dateParts = returnTime.split("-");
            int year = Integer.parseInt(dateParts[0]);
            int month = Integer.parseInt(dateParts[1]);
            int day = Integer.parseInt(dateParts[2]);
            String[] datePart = borrowTime.split("-");
            int year1 = Integer.parseInt(datePart[0]);
            int month1 = Integer.parseInt(datePart[1]);
            int day1 = Integer.parseInt(datePart[2]);

            money = (year - year1) * 365 + (month - month1) * 30 + (day - day1);
            return money;
        }
    }

    @Override
    public List<BookJl> getbook(String userName) throws LibraryException {
        List<BookJl> bookJlList = new ArrayList<>();
        User user = usermapper.getIdByName(userName);
        List<Ubjl> ubjls = bookmapper.getbookJl(user.getUserId());
        if (ubjls == null || ubjls.isEmpty()) {
            throw new LibraryException(ErrorCode.NOT_FOUND_RECODE);
        } else {
            for (Ubjl ubjl : ubjls) {
                BookJl bookJl = new BookJl();
                bookJl.setBorrowTime(ubjl.getBorrowTime());
                bookJl.setBookName(bookmapper.getbookAll(ubjl.getSid()).getTitle());
                bookJlList.add(bookJl);
            }
            return bookJlList;
        }
    }
}
